package ac.svn.accesscontrol.ui;

import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.SwingUtilities;

import ac.svn.accesscontrol.store.ISVNItem;

public class LocationFilter 
{
	private static final int FILTER_BY_LOCATION = 1;
	private String criteria = "";
	private Thread thread ;
	private JList targetList;
	private DefaultListModel listModel;
	private Set<ISVNItem> sourceList;
	public LocationFilter(String componentId,String model)
	{
		targetList = (JList) UIComponentLibrary.getComponent(componentId);
		listModel = (DefaultListModel) UIComponentLibrary.getComponent(model); 
				
		thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				listModel.clear();
				for(ISVNItem item: sourceList)
				{
					if(item.getName().toLowerCase().contains(criteria.toLowerCase()))
					{
						listModel.addElement(item);
					}
				}
				
				SwingUtilities.invokeLater(new Runnable() {
					
					@Override
					public void run() {
						SwingUtilities.updateComponentTreeUI(targetList);						
					}
				});
			}
		}) ;
	}
			
	public void filterList(Set<ISVNItem> sourceSet, String creteria) {
		this.sourceList = null;
		this.sourceList = sourceSet;
		this.criteria = creteria;
		if(thread.isAlive())
		{
			thread.interrupt();
		}
		thread.run();
	}
}
